package com.imy.virtual.client.stub;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.os.Build;
import android.os.IBinder;
import android.util.Log;

import com.imy.virtual.R;

import mirror.dalvik.system.VMRuntime;

/**
 * @author Lody
 */
public class DaemonService extends Service {

	public static final String CLASS_NAME="stub.DaemonService";
	private static final int NOTIFY_ID = 1001;

	public static void startup(Context context) {
		context.startService(new Intent(context, DaemonService.class));
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		startup(this);
	}

	private String channelId = "GameLoad";

	@Override
	public IBinder onBind(Intent intent) {
		return null;
	}

	private void createNotifyChannel() {

		String channelName = "GameLoad";
		int importance = 0;
		if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
			importance = NotificationManager.IMPORTANCE_HIGH;
			createNotificationChannel(channelId, channelName, importance);
		}
	}

	private void createNotificationChannel(String channelId, String channelName, int importance) {
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
			NotificationChannel channel = new NotificationChannel(channelId, channelName, importance);
			NotificationManager notificationManager = (NotificationManager) getSystemService(
					NOTIFICATION_SERVICE);
			// 设置通知出现时不震动
			channel.enableVibration(false);
			//channel.setVibrationPattern(new long[] { 0 });
			if (notificationManager != null) {
				notificationManager.createNotificationChannel(channel);
			}
			//notificationManager.getNotificationChannel(channelId);
		}
	}

	@Override
	public void onCreate() {
		super.onCreate();

		Log.i("DaemonService","onCreate");
	}

	private Notification getBuild() {
		Notification.Builder builder;
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
			builder = new Notification.Builder(this, channelId);
		} else {
			builder = new Notification.Builder(this);
		}
		Boolean call = VMRuntime.is64Bit.call(VMRuntime.getRuntime.call());
		String text ;
		if (call){
			text="64support";
		}else {
			text="AndroidLuaScript";
		}
		builder.setContentTitle(text)
				.setContentText(text)
				.setWhen(System.currentTimeMillis())
				.setAutoCancel(true)
				.setVibrate(new long[0])
				.build();
		return builder.build();
	}

	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		createNotifyChannel();
		startForeground(NOTIFY_ID, getBuild());
		Log.i("DaemonService","onStartCommand");
		return super.onStartCommand(intent,flags,startId);
	}


}
